/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package com.opensymphony.user.provider.hibernate3.ahxu.impl;

import com.opensymphony.user.provider.hibernate3.ahxu.dao.HibernateQueries;
import com.opensymphony.user.provider.hibernate3.ahxu.dao.HibernateUserDAO;
import com.opensymphony.user.provider.hibernate3.ahxu.dao.SessionManager;
import com.opensymphony.user.provider.hibernate3.ahxu.entity.HibernateUser;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.type.Type;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.Serializable;
import java.util.ArrayList;

import java.util.List;
import org.hibernate.impl.SessionImpl;


/**
 * @author <a href="mailto:matthew@linjafoo.com">Matthew E. Porter</a>
 */
public class HibernateUserDAOImpl implements HibernateUserDAO {
    //~ Instance fields ////////////////////////////////////////////////////////

    protected Log log = LogFactory.getLog(HibernateUserDAOImpl.class.getName());
    private SessionManager sessionManager;

    //~ Constructors ///////////////////////////////////////////////////////////

    public HibernateUserDAOImpl(SessionManager sessionManager) {
        this.sessionManager = sessionManager;
    }

    //~ Methods ////////////////////////////////////////////////////////////////

    public int deleteUserByUsername(String username) {
        int numberOfUsersDeleted = 0;
        SessionImpl session = null;//ahxu

        try {
            session = (SessionImpl)this.sessionManager.getSession();
            numberOfUsersDeleted = session.delete(HibernateQueries.USER_BY_USERNAME, username, Hibernate.STRING);
        } catch (HibernateException he) {
            if (log.isDebugEnabled()) {
                log.debug("Unable to find user with username " + username, he);
            }
        } finally {
            this.sessionManager.closeSession(session);
        }

        return numberOfUsersDeleted;
    }

    public HibernateUser findUserByUsername(String username) {
        HibernateUser user = null;
        SessionImpl session = null;//ahxu

        try {
            session = (SessionImpl)this.sessionManager.getSession();

            List ret = session.find(HibernateQueries.USER_BY_USERNAME, username, Hibernate.STRING);
            if(ret==null) ret=new ArrayList();
            if (ret.size() > 0) {
                user = (HibernateUser) ret.get(0);
            }
        } catch (HibernateException he) {
            if (log.isDebugEnabled()) {
                log.debug("Unable to find user with username " + username, he);
            }

            user = null;
        } finally {
            this.sessionManager.closeSession(session);
        }

        return user;
    }

    public HibernateUser findUserByUsernameAndGroupname(String userName, String groupName) {
        SessionImpl session = null;//ahxu
        HibernateUser user = null;

        try {
            session = (SessionImpl)this.sessionManager.getSession();

            List results = session.find(HibernateQueries.USER_BY_USERNAME_AND_GROUPNAME, new Object[] {
                    userName, groupName
                }, new Type[] {Hibernate.STRING, Hibernate.STRING});
            if(results==null) results=new ArrayList();
            user = (HibernateUser) results.get(0);
        } catch (HibernateException he) {
            if (log.isDebugEnabled()) {
                log.debug("Unable to find user with username " + userName + " and groupname " + groupName, he);
            }

        } finally {
            this.sessionManager.closeSession(session);
        }

        return user;
    }

    public List findUsers() {
        List users = new ArrayList();
        SessionImpl session = null;

        try {
            session = (SessionImpl)this.sessionManager.getSession();
            List found = session.find(HibernateQueries.ALL_USERS);
            if(found!=null) users=found;
        } catch (HibernateException he) {
            if (log.isDebugEnabled()) {
                log.debug("HibernateException retrieving all groups.", he);
            }
        } finally {
            this.sessionManager.closeSession(session);
        }

        return users;
    }

    public boolean saveUser(HibernateUser user) {
        boolean result = false;
        Serializable id = null;
        Session session = null;

        try {
            session = this.sessionManager.getSession();
            id = session.save(user);
            result = (id != null);
        } catch (HibernateException he) {
            if (log.isDebugEnabled()) {
                log.debug("HibernateException caught saving");
            }

            result = false;
        } finally {
            this.sessionManager.flushCloseSession(session);
        }

        return result;
    }

    public boolean updateUser(HibernateUser user) {
        boolean result = false;
        Session session = null;

        try {
            session = this.sessionManager.getSession();
            session.saveOrUpdate(user);
            result = true;
        } catch (HibernateException he) {
            if (log.isDebugEnabled()) {
                log.debug("Unable to update user with username " + user.getName(), he);
            }

            result = false;
        } finally {
            this.sessionManager.flushCloseSession(session);
        }

        return result;
    }
}
